package com.byzk.p2p.admin.money.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.BaseUtil;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.borrow.dao.TenderDao;
import com.byzk.p2p.admin.borrow.vo.ChangeAwaitVo;
import com.byzk.p2p.admin.money.dao.FundRecordDao;
import com.byzk.p2p.admin.money.dao.WithdrawDao;
import com.byzk.p2p.admin.money.dto.FundRecord;
import com.byzk.p2p.admin.money.dto.WithdrawRecord;
import com.byzk.p2p.admin.money.util.FundRecordType;
import com.byzk.p2p.admin.money.vo.SumWithdrawVo;
import com.byzk.p2p.admin.money.vo.WithdrawRecordExportVo;
import com.byzk.p2p.admin.money.vo.WithdrawRecordVo;
import com.byzk.p2p.admin.money.vo.WithdrawUserVo;
import com.byzk.p2p.admin.user.dao.UserAccountDao;
import com.byzk.p2p.admin.user.dto.UserAccount;

@Service
public class WithdrawService {
	
	@Autowired
	private WithdrawDao withdrawDao;
	
	@Autowired
	private TenderDao tenderDao;
	
	@Autowired
	private UserAccountDao accountDao;
	
	@Autowired
	private FundRecordDao fundRecordDao;
	
	@Autowired
	private RechargeService rechargeService;
	

	
	/**
	 * 根据状态查询提现列表
	 * @param status 提现状态
	 * @return
	 */
	
	public PageInfo<WithdrawRecordVo> listWithdrawByStatus(WithdrawRecordVo vo){
		int count=withdrawDao.countByStatus(vo);
		List<WithdrawRecordVo> userList=withdrawDao.listByStatus(vo);
		vo.setStatus("success");
		SumWithdrawVo withdraw=withdrawDao.sumByStatus(vo);
		PageInfo<WithdrawRecordVo> page=new PageInfo<WithdrawRecordVo>(count, vo.getIndex() , vo.getLimit());
		if(withdraw==null){
			page.setSum(0.0);
			page.setSumFee(0.0);
			page.setSumBlance(0.0);			
		}else{
			page.setSum(withdraw.getSumTotal());
			page.setSumFee(withdraw.getSumFee());
			page.setSumBlance(withdraw.getSumBalance());			
		}
		page.setPageList(userList);
		return page;
	}
	
	/**
	 * 新增 or 更新
	 * @param record
	 * @return
	 */
	public Integer saveOrUpdate(WithdrawRecord record){
		if(record.getId()==null){
			return withdrawDao.insert(record);
		}else{
			return withdrawDao.update(record);
		}
	}
	
	/**
	 * 根据用户id获取提现用户的详细信息
	 * @param uid 用户id
	 * @return
	 */
	public WithdrawUserVo getWithdrawUserDetail(Integer uid){
		ChangeAwaitVo await=tenderDao.sumChangeAwait(uid,null);
		UserAccount account=accountDao.getByUserId(uid);
		WithdrawUserVo userVo=withdrawDao.getWithdrawUserDetail(uid);
		userVo.setAwait(tenderDao.sumAwaitTotal(uid)+await.getPaymentTotal()+account.getAwait());
		return userVo;
	}
	
	public WithdrawRecord getWithdrawDetail(Integer id){
		return withdrawDao.getWithdrawDetail(id);
	}
	
	public List<WithdrawRecordVo> exportList(WithdrawRecordExportVo vo){
		return withdrawDao.listByExportVo(vo);
	}
	
	public List<WithdrawRecord> listByParams(WithdrawRecord record) {
		return withdrawDao.listByParams(record);
	}
	
	public List<WithdrawRecordVo> listByIds(List<String> ids){
		return withdrawDao.listByIds(ids);
	}

	public SumWithdrawVo sumByStatus(WithdrawRecordVo vo){
		return withdrawDao.sumByStatus(vo);
	}
	
	public Double sumTotalWithdarw() {
		return withdrawDao.sumTotalWithdarw();
	}
	
	public SumWithdrawVo sumByDateDanger(Date start,Date end){
		return withdrawDao.sumByDateDanger(start,end);
	}
	
	/**
	 * 转账
	 * @param detail 转账记录
	 * @return 
	 */
	public boolean transfer(WithdrawRecord detail){
		Integer result = withdrawDao.update(detail);
		if (result > 0) {
			UserAccount account=accountDao.getByUserId(detail.getUserId());
			if("apply_failure".equals(detail.getStatus()) || "failure".equals(detail.getStatus())){
				FundRecord record=new FundRecord();
				record.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
				record.setIncome(detail.getMoney());
				record.setExpend(0.00);
				record.setType(FundRecordType.Type.UNFREEZEN.index);
				record.setUserId(detail.getUserId());
				record.setRemark("提现失败");
				record.setBalance(account.getBalance()+detail.getMoney());
				record.setFreezen(0.0);
				record.setCreateTime(new Date());
				fundRecordDao.insert(record);
				
				UserAccount account1=accountDao.getByUserId(detail.getUserId());
				account1.setFreezen(account1.getFreezen()-detail.getMoney());
				account1.setBalance(account1.getBalance()+detail.getMoney());
				accountDao.update(account1);
			}
			if("success".equals(detail.getStatus())){
				
				FundRecord record=new FundRecord();
				record.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
				record.setIncome(0.00);
				record.setExpend(detail.getMoney());
				record.setType(FundRecordType.Type.WITHDRAW_UNFREEZEN.index);
				record.setUserId(detail.getUserId());
				record.setRemark("提现成功");
				record.setBalance(account.getBalance());
				record.setFreezen(0.0);
				record.setCreateTime(new Date());
				fundRecordDao.insert(record);
				
				UserAccount account2=accountDao.getByUserId(detail.getUserId());
				account2.setFreezen(account2.getFreezen()-detail.getMoney());
				account2.setTotalAmount(account2.getTotalAmount()-detail.getMoney());
				account2.setTotalWithdraw(account2.getTotalWithdraw()+detail.getMoney());
				
				accountDao.update(account2);
			}
			
			return true;
		} else {
			return false;
		}
	}
	
	public List<WithdrawRecord> selectRecordByCode(String id){
		return withdrawDao.getRecordByCode(id);
	}
	
	/**
	 * @param code 编号
	 * @param userId 提现用户ID
	 * @param amount 提现金额
	 * @param fee 手续费金额
	 * @param free 是否免费提现
	 * @return
	 */
	public Integer addWithdraw(String code,Integer userId,double amount,Double fee){
		UserAccount account=accountDao.getByUserId(userId);
		
		rechargeService.addFundRecord(userId,0.0,amount,"PC","提现",FundRecordType.Type.WITHDRAW.index,0.0,account.getBalance()-amount);
		
		//用户账户表
		account.setTotalAmount(account.getTotalAmount()-amount);
		account.setBalance(account.getBalance()-amount);
		account.setTotalWithdraw(account.getTotalWithdraw()+amount);//TODO 总提现金额减去了手续费
		//免提现额度
//		Double used_money=account.getBalance()-account.getNotUsedMoney();
		
//		if(amount>used_money){//提现金额大于可提现金额
//			account.setNotUsedMoney(0.00);
//		}else {
//			account.setNotUsedMoney(used_money-amount);
//		}
		
		accountDao.update(account);
		
		return null;
	}

	public List<WithdrawRecord> listByDateAndUid(Integer uid,String date){
		return withdrawDao.listByDateAndUid(uid,date);
	}
}
